package com.liangzhong.proxy.fameHall
{
	import com.liangzhong.commandconst.commandType.MainCommandType;
	import com.liangzhong.commandconst.fameHall.SMDFameHall;
	import com.liangzhong.constants.HttpRequestConst;
	import com.liangzhong.errortip.ErrorTips;
	import com.liangzhong.global.CMD_Head;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.FameHall.vo.FameHallVo;
	import com.liangzhong.proxy.BaseProxy;
	import com.liangzhong.proxy.ProxyUtils;
	import com.liangzhong.proxyconstant.fameHall.FameHallProxyConstant;
	import com.socket.clientsocket.IClientSocket;
	
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	import lz.controls.Alert;
	
	public class FameHallProxy extends BaseProxy
	{
		public static const PROXYNAME:String = "FameHallProxy";
		
		/**
		 *放置的球员 honourid  cellid  ntype
		 */
		private var _putPlayer:Object;
		public function FameHallProxy(proxyName:String=PROXYNAME, data:Object=null)
		{
			super(proxyName, data);
		}
		/**接受 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @param clientSocket
		 * @return 
		 * 
		 */		
		override public function onSocketRead(mainCmdID:uint, subCmdID:uint, buffer:ByteArray, dataSize:int, clientSocket:IClientSocket):Boolean
		{
			switch(mainCmdID)
			{
				case MainCommandType.MAINDEAL:
					return onDeal(subCmdID,buffer,dataSize);
					break;
				case MainCommandType.TASK:
					return onTaskDeal(subCmdID,buffer,dataSize);
					
			}
			return false;
		}
		
		private function onTaskDeal(subCmdID:uint,
									buffer:ByteArray, 
									dataSize:int):Boolean
		{
			switch(subCmdID)
			{
				case SMDFameHall.GETAWARD:
					getGetAward(buffer);
					return true;
			}
			return false;
			
		}
		/**
		 *处理 
		 * @param mainCmdID
		 * @param subCmdID
		 * @param buffer
		 * @param dataSize
		 * @return 
		 * 
		 */		
		private function onDeal(subCmdID:uint,
								buffer:ByteArray, 
								dataSize:int):Boolean
		{
			switch(subCmdID)
			{
				case SMDFameHall.PUTPLAYER:
					getPutPlayer(buffer);
					return true;
			}
			return false;
		}
		
		/**
		 * 请求放置球员
		 * @param $obj
		 * 
		 */
		public function requestPutPlayer($obj:Object):void
		{
			_putPlayer = $obj;
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 7,MainCommandType.MAINDEAL,SMDFameHall.PUTPLAYER);
			var id:int = $obj.id;
			sendBuf.writeInt($obj.honourid);
			sendBuf.writeShort($obj.cellid);
			sendBuf.writeByte($obj.ntype);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回旋转球员 
		 * @param buffer
		 * 
		 */
		private function getPutPlayer(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn == 0)
			{
				var active:int = buffer.readByte();
				if(_putPlayer != null)
				{
					_putPlayer.active = active;
					AppData.getInstance().fameHallDateManager.putPlayer(_putPlayer,active);
					
				}
				this.sendNotification(FameHallProxyConstant.GET_PUTPLAYER,_putPlayer);
//				trace("是否激活：" + active);
				Alert.showErrorTip("球员放置成功");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				this.sendNotification(FameHallProxyConstant.GET_PUTPLAYER,null);
			}
		}
		
		/**
		 *请求荣誉传信息 
		 * 
		 */
		public function requestFameHallInfo():void
		{
			var variables:URLVariables = ProxyUtils.instance.getUrlVariables();
			variables.method = HttpRequestConst.FAMEHALL_INFO;
			ProxyUtils.instance.webClient(getFameHallInfo,variables,false);
		}
		/**
		 *返回荣誉信息 
		 * @param $obj
		 * 
		 */
		private function getFameHallInfo($data:Object):void
		{
			var obj:Object = ProxyUtils.instance.analysisHttpData($data,FameHallProxy);
			if(obj.retcode == 0)
			{
				//				_clubDataManager.analysisClubInfo(obj);
				AppData.getInstance().fameHallDateManager.getFameHallInfo(obj.honour);
				this.sendNotification(FameHallProxyConstant.GET_FAMEHALLINFO);
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getErrorTip(obj.retcode));
			}
		}
		
		/**
		 *请求领取奖励 
		 * @param $obj
		 * 
		 */
		public function requestGetAward($obj:Object):void
		{
			var sendBuf :ByteArray = 
				CMD_Head.getHeadByteArrayBuff(CMD_Head.Size + 4,MainCommandType.TASK,SMDFameHall.GETAWARD);
			sendBuf.writeInt($obj.honourid);
			sendBuf.position = 0;
			clientSocket.sendData(sendBuf,sendBuf.bytesAvailable);
		}
		/**
		 *返回领取奖励 
		 * @param buffer
		 * 
		 */
		private function getGetAward(buffer:ByteArray):void
		{
			var nReturn:int = buffer.readInt();
			if(nReturn == 0)
			{
				var honourid:int = buffer.readInt();
				var status:int = buffer.readByte();
				var vo:FameHallVo = AppData.getInstance().fameHallDateManager.getFameHallVo(honourid);
				vo.status = status;
				this.sendNotification(FameHallProxyConstant.GET_GETAWARD,{vo:vo});
				//				trace("是否激活：" + active);
				Alert.showErrorTip("领取成功！");
			}else
			{
				Alert.showErrorTip(ErrorTips.instance.getCErrorTip(nReturn));
				this.sendNotification(FameHallProxyConstant.GET_GETAWARD,null);
			}
		}
	}
}